package com.example.common.OSS;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.example.common.Util.MD5Util;

import java.util.Calendar;

public class OssHelper {

    private static final String endpoint ="oss-cn-shenzhen.aliyuncs.com";
    private static final String bucketName="lztoos";
    private static String accessKeyId="LTAI4FpPkdsaVLnkkSC7QSoW";
    private static String accessKeySecret="BsGtk68sc9MxkUSxm0jDhNPn1RdY3n";

    private static OSS getClient(Context context){

        //直接保存accessKeyId，accessKeySecret的方法不安全
        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret);
        //OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
        return new OSSClient(context, endpoint, credentialProvider);
    }



    /**
     * 上传方法，成功返回路径
     * @param objKey 服务器上的独立key
     * @param path 上传文件路径
     * @return 文件地址
     */
    private static String upload(Context context,String objKey, String path){
        // 构造上传请求。
        OSS oss=getClient(context);
        PutObjectRequest request = new PutObjectRequest(bucketName, objKey, path);
        try {
            PutObjectResult putResult = oss.putObject(request);
            Log.d("ETag", putResult.getETag());
            Log.d("RequestId", putResult.getRequestId());
            return objKey;
        } catch (ClientException e) {
            // 本地异常，如网络异常等。
            System.out.println("netErrornetErrornetErrornetErrornetErrornetErrornetErrornetErrornetErrornetError");
            e.printStackTrace();
        } catch (ServiceException e) {
            // 服务异常。
            System.out.println("serviewErrorserviewErrorserviewErrorserviewErrorserviewErrorserviewErrorserviewErrorserviewError");
            Log.e("RequestId", e.getRequestId());
            Log.e("ErrorCode", e.getErrorCode());
            Log.e("HostId", e.getHostId());
            Log.e("RawMessage", e.getRawMessage());
        }
        return "errorUpload";
    }
//        try{
//            //初始化上传的Client
//            OSS oss=getClient(context);
//            //开始同步上传
//            PutObjectResult result = oss.putObject(request);
//            //得到外网可访问地址
//            String url = oss.presignPublicObjectURL(bucketName,objKey);
//            System.out.println("得到外网可访问地址");
//
//            return url;
//
//        }catch (Exception e){
//            e.printStackTrace();
//            //有异常返回空
//            return null;
//        }





    /**
     * 上传普通图片
     * @param path 文件路径
     * @return 服务器路径
     */
    public static String uploadImage(Context context,String path){
        String key=getImageObjKey(path);
        return upload(context,key,path);
    }

    /**
     * 上传视频
     * @param path
     * @return
     */
    public static String uploadVideo(Context context,String path){
        String key=getVideoObjKey(path);
        return upload(context,key,path);
    }

    private static String getDateString(){
        Calendar c = Calendar.getInstance();
        String mYear=c.get(Calendar.YEAR)+"";
        String mMonth=c.get(Calendar.MONTH)+1+"";
        if(mMonth.length()==1){
            mMonth="0"+mMonth;
        }
        return mYear+mMonth;
    }

    //image/201909/lahkegaaeg58d7agaf.jpg
    private static String getImageObjKey(String path){
        String fileMd5= MD5Util.md5(path);
        fileMd5=fileMd5.substring(0,fileMd5.length()-4);
        String dateString=getDateString();
        System.out.println("getPathKeySucess"+String.format("image/%s/%s.jpg",dateString,fileMd5));
        return String.format("image/%s/%s.jpg",dateString,fileMd5);
    }

    //video/201909/lahkegaaeg58d7agaf.mp4
    private static String getVideoObjKey(String path){
        String fileMd5= MD5Util.md5(path);
        String dateString=getDateString();
        return String.format("video/%s/%s.jpg",dateString,fileMd5);
    }
}